<?php
require_once 'dao/SequenceDAO.php';
require_once 'dao/MeetingDAO.php';
require_once 'dao/ParticipantDAO.php';
require_once 'dao/TagDAO.php';
require_once 'AbstractObservableService.php';

class CreateMeetingService extends AbstractObservableService{
	public static $SERVICE_ID = "createMeeting";
	public function doService($requestId, $params){
		$sequenceDAO = new SequenceDAO();
		$meetingId = $sequenceDAO->createNewId(MeetingDAO::$MEETING_ID_PREFIX);
		$regionId = $sequenceDAO->createNewId(MeetingDAO::$REGION_ID_PREFIX);
		$params["meetingId"] = $meetingId;
		
		$meetingDAO = new MeetingDAO();
		$meeting = $params;
		$meeting["meetingId"] = $meetingId;
		$meeting["regionId"] = $regionId;
		$meeting["orgName"] = $meeting["organizer"]["name"];
		$meeting["orgPhoneNo"] = $meeting["organizer"]["phoneNo"];
		$meeting["regionX"] = $meeting["region"]["x"];
		$meeting["regionY"] = $meeting["region"]["y"];
		$meetingDAO->insertMeeting($meeting);
		
		$clientIdList = array();
		$participantDAO = new ParticipantDAO();
		$participants = $params["participants"];
		if ($participants != null){
			foreach ($participants as $participant){
				$participant["meetingId"] = $meetingId;
				$participantDAO->insertParticipant($participant);
				array_push($clientIdList, $participant["phoneNo"]);
			}
		}
		
		$tagDAO = new TagDAO();
		$tagNames = $params["region"]["tags"];
		if ($tagNames != null){
			foreach($tagNames as $tagName){
				$tag = array();
				$tag["tagId"] = $regionId;
				$tag["tagName"] = $tagName;
				$tagDAO->insertTag($tag);
			}
		}
		
		$this->insertObserverUpdate(CreateMeetingService::$SERVICE_ID, $requestId, $params, $params["appointmentTime"], $clientIdList);
		return null;
	}
}
?>